/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditItem.java
 *
 * Created on Nov 5, 2011, 4:23:48 PM
 */
package mpinventory;

import java.sql.*;
import java.util.List;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author msi
 */
public class EditItem extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    public ItemQuery itemQuery;
    private List<Item> items;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    private String quantityUnit;
    /** Creates new form EditUser */
    public EditItem(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        initializeItemList();
        cbxInvEUnit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent actionEvent){
                quantityUnit=cbxInvEUnit.getSelectedItem().toString();
            }
        });
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtInvEDescription = new javax.swing.JTextField();
        cbxInvEUnit = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        txtInvEQuantity = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnInvESave = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtInvEItemID = new javax.swing.JTextField();
        txtInvEPrice = new javax.swing.JTextField();
        btnInvECancel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25)); // NOI18N
        jLabel1.setText("Edit Item");

        txtInvEDescription.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        cbxInvEUnit.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxInvEUnit.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "roll", "length", "pcs.", "ft.", "meters", "box", "sets", "pairs", "kl.", "cans", "gal.", "pint" }));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Quantity:");

        txtInvEQuantity.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Description:");

        btnInvESave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        btnInvESave.setText("Save");
        btnInvESave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15)); // NOI18N
        jLabel5.setText("Unit Price:");

        txtInvEItemID.setEditable(false);
        txtInvEItemID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        txtInvEPrice.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        btnInvECancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnInvECancel.setText("Cancel");
        btnInvECancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Item ID:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtInvEItemID, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtInvEQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbxInvEUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtInvEPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtInvEDescription))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(154, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(133, 133, 133))
            .addGroup(layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(btnInvESave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56)
                .addComponent(btnInvECancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(81, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtInvEItemID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(46, 46, 46)))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtInvEDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtInvEQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxInvEUnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(txtInvEPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInvESave)
                    .addComponent(btnInvECancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initializeItemList(){
        itemQuery=new ItemQuery(conn);
        items=itemQuery.initializeItems();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<items.size();i++)
            listModel.addElement(items.get(i).getDescription());
        listItem.setModel(listModel);
        int j=listItem.getSelectedIndex()+1; //+1 because index of lists starts at -1
        txtInvEDescription.setText(items.get(j).getDescription());
        txtInvEItemID.setText(items.get(j).getID()+"");
        txtInvEPrice.setText(items.get(j).getUnitPrice()+"");
        txtInvEQuantity.setText(items.get(j).getQuantity()+"");
        int k=0;
        cbxInvEUnit.setSelectedIndex(k);
        while(true){
            if(cbxInvEUnit.getSelectedItem().equals(items.get(j).getQuantityUnit()))
                break;
            else{
                k++;
                cbxInvEUnit.setSelectedIndex(k);
            }
        }
        listItem.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e){
                try{
                    int j=listItem.getSelectedIndex();
                    items=itemQuery.initializeItems();
                    txtInvEDescription.setText(items.get(j).getDescription());
                    txtInvEItemID.setText(items.get(j).getID()+"");
                    txtInvEPrice.setText(items.get(j).getUnitPrice()+"");
                    txtInvEQuantity.setText(items.get(j).getQuantity()+"");
                    int k=0;
                    cbxInvEUnit.setSelectedIndex(k);
                    while(true){
                        if(cbxInvEUnit.getSelectedItem().equals(items.get(j).getQuantityUnit()))
                            break;
                        else{
                            k++;
                            cbxInvEUnit.setSelectedIndex(k);
                        }
                    }
                }catch(Exception f){}
            }
        });
    }
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
        Object[] options=new String[]{"Yes","No"};
        pane.setOptions(options);
        JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
        dialog.show();
        if(options[0].equals(pane.getValue())) //YES
            backToMainMenu=true;
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        itemQuery=new ItemQuery(conn);
        if(txtInvEDescription.getText().equals("") || txtInvEPrice.getText().equals("") || txtInvEQuantity.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to edit this item?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Edition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                itemQuery.editItem(Integer.parseInt(txtInvEItemID.getText()),Float.parseFloat(txtInvEQuantity.getText()),quantityUnit,txtInvEDescription.getText(),Float.parseFloat(txtInvEPrice.getText()),listItem.getSelectedValue().toString());
                int j=listItem.getSelectedIndex();
                DefaultListModel listModel = new DefaultListModel();
                items=itemQuery.initializeItems();
                for(int i=0;i<items.size();i++)
                    listModel.addElement(items.get(i).getDescription());
                listItem.setModel(listModel);
                listItem.setSelectedIndex(j); //update item list
            } //YES
        }
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInvECancel;
    private javax.swing.JButton btnInvESave;
    private javax.swing.JComboBox cbxInvEUnit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtInvEDescription;
    private javax.swing.JTextField txtInvEItemID;
    private javax.swing.JTextField txtInvEPrice;
    private javax.swing.JTextField txtInvEQuantity;
    // End of variables declaration//GEN-END:variables
}
